module(..., package.seeall)

local localGroup = display.newGroup()
local physics = require ("physics")

require ("dialog")

physics.start()

local showItems = {}

local player
local redo
local trigger
local toggle=0
local counter = 1

local StartXShoot = 200
local startYShoot = 305

local lastTimerUpdate
local deltaTime
local showTimeElapsed = 0
local redoOn = false
local currentTime
local touchOn = false
local touchTicks = 1
local showOn = false

local function showTimer(event)
	if(showOn == true) then
		currentTime = os.time()
		deltaTime = currentTime - lastTimerUpdate
		lastTimerUpdate = currentTime
		showTimeElapsed = showTimeElapsed + deltaTime

		if(showTimeElapsed >= 2) then
			if(redoOn == false) then
				redoOn = true
				redo.isVisible = true
			end
		end
	end

	if(touchOn == true) then
		touchTicks = touchTicks + 1
	end
end

-- dunno wtf these two are TODO
local dialog1
local instructions


local closeDialog = 
function()
	dialog.close(dialog1)

	director:changeScene("screen1")
	
end

local closeInstructions = 
function()
	dialog.close(instructions)
	
end

local closeDialog = 
function()
	dialog.close(dialog1)

	director:changeScene("screen1")
	
end

local function onLocalCollision (self, event)
	print("collision")
	if(event.phase =="ended") then
		if(self.myName == "goal") then
			if(event.other.myName == "player") then
				-- if the player and the goal collide
				print("player hit goal")
				if(hitGoal == 0) then
					self.isVisible = false
					hitGoal = 1
					dialog1 = dialog.new
					{
						contentTopMargin = 35,
						contentWidthMargin=15,
						background = 'dialog.png',
						text = [[COMPLETE]],
						textColor = {0,0,0},
						backgroundColor = {0,0,200},
						buttons = {
								{text = "ok", color={255, 255, 0}, onRelease=closeDialog, default='button-ok.png', over='button-ok.png'} 
						}
					}
				end
			end
		elseif(self.myName == "whiteball") and (toggle == 1) then
			print("hit the white ball!")
			--TODO while the collision is resolving cant move the
			--player. fint a way to "bounce"
		elseif(self.myName == "blackball") and (toggle == 0) then
			print([[hit the black ball]])
			buttons = {
				{text = "ok", color={255, 255, 0}, onRelease=closeDialog, default='button-ok.png', over='button-ok.png'} 

			} 
		end
	elseif(event.phase=="ended") then
	end
end

local function onBoundsCollision( self, event )
        if ( event.phase == "ended" ) then
		print("Bounds collision")
        end
end

-- Main function
function new()

	--Background
	local background = display.newImage("genericbg.png")
	localGroup:insert(background)

	local gameGroup = display.newGroup()

	local ground = display.newImage( "ground.png" )
	ground.x = ground.width/2
     	ground.y = 345
	ground.myName = "ground"
	physics.addBody( ground, "static", { friction=0.5, bounce=0.1 } )
	gameGroup:insert(ground)

	local ceiling = display.newImage( "ground.png" )
	ceiling.x = ground.width/2
    ceiling.y =0	
	ceiling.myName = "ceiling"
	physics.addBody(ceiling, "static", { friction=0.5, bounce=0.1 } )
--   	ceiling.collision = onBoundsCollision
   	ceiling:addEventListener("collision", ceiling) 	gameGroup:insert(ceiling)

	local leftbounds = display.newImage( "verticalbar.png" )
	leftbounds.x = -10
     	leftbounds.y = leftbounds.height/2
	leftbounds.myName = "leftbounds"
	physics.addBody( leftbounds, "static", { friction=0.5, bounce=0.1 } )
	gameGroup:insert(leftbounds)

	local rightbounds = display.newImage( "verticalbar.png" )
	rightbounds.x = 490
     	rightbounds.y = leftbounds.height/2
	rightbounds.myName = "rightbounds"
	physics.addBody(rightbounds, "static", { friction=0.5, bounce=0.1 } )
	gameGroup:insert(rightbounds)

function redot(event)
	self = event.target
	if (event.phase == "ended") then

		player.x = startXShoot
		player.y = startYShoot
		redo.isVisible = true
		redoOn = false
		showOn = true
		touchTicks = 1
	end
end

function triggert(event)
	self = event.target
	if(event.phase == "began") then
		print("pressed the trigger!")
		toggle = 1
		print("the toggle is " .. toggle)
	elseif(event.phase == "ended") then
		print("released the trigger!")
		toggle = 0
		print("the toggle is " .. toggle)
	end
end

function playert(event)
	self = event.target

	if(event.phase == "ended") then
		touchOn = false
		showOn = true
		showTimeElapsed = 0
		lastTimerUpdate = os.time()

		print("touch tick is "..touchTicks)

	elseif (event.phase == "moved") then
		self.x = event.x
		self.y = event.y
		
	elseif (event.phase == "began") then
		self.isFocus = true
		touchTicks = 1
		touchOn = true
	end
	return true
end	


	print("make the white ball")
	showItems[counter] = display.newImage("whiteball.png")
	showItems[counter].x = 100 showItems[1].y = 200
	showItems[counter].myName = "whiteball"
	showItems[counter].collision = onLocalCollision
	showItems[counter]:addEventListener("collision", showItems[1])
	physics.addBody(showItems[counter], "static", { radius = 1, friction = 0.5, bounce = 0.3, isSensor = true} )
	gameGroup:insert(showItems[counter])
	
	print("make the black ball")
	for i = counter, 8 do
		print([[the iterator is ]] .. i)
		showItems[i] = display.newImage("blackball.png")
		showItems[i].x = i*50 showItems[counter].y = 100
		showItems[i].myName = "blackball"
		showItems[i].collision = onLocalCollision
		showItems[i]:addEventListener("collision", showItems[i])
		physics.addBody(showItems[i], "static", { radius = 1, friction = 0.5, bounce = 0.3, isSensor = true} )
		gameGroup:insert(showItems[i])
	end
	counter = 10 + 1
	
	print("make the goal")
	showItems[counter] = display.newImage("goal.png")
	showItems[counter].x = 350 showItems[counter].y = 150
	showItems[counter].myName = "goal"
	showItems[counter].collision = onLocalCollision
	showItems[counter]:addEventListener("collision", showItems[counter])


	print("make the trigger button")
	trigger = display.newImage("trigger.png")
	trigger.x = 30 trigger.y = 290
	trigger.myName = "trigger"
	trigger.isVisible = true
	gameGroup:insert(trigger)
	trigger:addEventListener("touch", triggert)

	showItems[counter] = display.newImage( [[player.png]] )
	showItems[counter].x = 220
	showItems[counter].y = 315
	showItems[counter].myName = "player"
	
	physics.addBody(showItems[counter], "dynamic", { density=0.5, friction = 0.5, bounce = 0.0} )
	gameGroup:insert(showItems[counter])
	player = showItems[counter]
	player:addEventListener("touch", playert)

	redo = display.newImage("undo.png")
	redo.x = 450
	redo.y = 290
	redo.isVisible = true
	redo.myName = "redo"
	gameGroup:insert(redo)
	redo:addEventListener("touch", redot)

	instructions = dialog.new
	{
		contentTopMargin=35,
		contentWidthMargin=15,
		background='dialog.png',
		text=[[Help Daniel make his way to his lost toy. Avoid black balls by pressing the button and avoid white balls by releasing the button]],
		textColor={0,0,0},
		backgroundColor={0,0,200},
		backgroundStrokeColor={0,0,255},
		buttons={
			{text="ok", color={255,255,0}, onRelease=closeInstructions, default='button-ok.png', over='button-ok.png'}
		}
	}

	Runtime:addEventListener("enterFrame",showTimer)

	localGroup:insert(gameGroup)

	return localGroup

end
